package com.ld.security.common.core.util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 文件操作工具类
 */
public class FileUtil {



    public static void mkdirs(File f){
        if(f.exists()){
            f.mkdirs();
        }
    }
    public static void mkdirs(List<File> fs){
        for (File f : fs) {
            mkdirs(f);
        }
    }
    @SneakyThrows
    public static void writeByte(File f,byte[] bs){
        FileOutputStream out =   new FileOutputStream(f);
        out.write(bs);
        out.flush();
        out.close();
    }

    @SneakyThrows
    public static    byte[] readBytes(File f){
        FileInputStream in =  new FileInputStream(f);
        byte[] bs = IoUtil.readBytes(in);
        in.close();
        return bs;
    }

    /**
     * 判断文件夹是否被写入中
     *
     * @param f
     * @return
     */
    public static boolean checkWrite(File f){
            for (File file : getAllFile(f)) {

                if(!file.renameTo(file)){
                    return true;
                }
            }
            return false;

    }


    private static void getAllFile(File f,List<File> fs){
        if(f.isFile()){
            fs.add(f);
        }else{
            for (File file : f.listFiles()) {
                //
                getAllFile(file,fs);
            }
        }
    }

    /**
     * 得到所有文件
     * @param f
     * @return
     */
    public static List<File> getAllFile(File f){
        LinkedList list =   new LinkedList<>();
        getAllFile(f,list);
        return list;
    }

    /**
     * 删除文件夹
     * @param source
     */
    public static void deleteFolder(File source) {
        for (File file : getAllFile(source)) {
            file.delete();
        }
    }
        /**
         * 文件夹移动
         * @param source
         * @param target
         */
        @SneakyThrows
    public static void moveFolder(File source, File target){
        for (File file : getAllFile(source)) {
            String newPath = file.getPath();
            newPath =   newPath.replace(source.getPath(),target.getPath());
            File newFile = new File(newPath);
            newFile.getParentFile().mkdirs();
            if(!newFile.exists()){
                newFile.createNewFile();
            }

            FileOutputStream out =  new FileOutputStream(newFile);
            FileInputStream inputStream =            IoUtil.toStream(file);
            IOUtils.copy(inputStream,out);
            inputStream.close();
            out.close();
        }
    }


    /**
     * 解析文件夹 根据后缀分类
     * @param f
     */
    public static Map<String,List<File>> parseFloderByType(File f) {
        Map<String,List<File>> map =   new TreeMap<>();
        for (File file : getAllFile(f)) {
            String key = getSuffix(f.getName());
            List<File> list = map.get(key);
            if(list == null){
                list = new LinkedList<>();
                map.put(key,list);
            }
            list.add(file);
        }

        return map;
    }

    /**
     * 获得文件后缀
     * @param name
     * @return
     */
    public static String getSuffix(String name){
        int start= name.lastIndexOf(".");
        if(start == -1){
            return name;
        }
        return name.substring(start,name.length());
    }

    public static void  main(String[] args){
        File x = new File("F:/fip/upload");
        File y = new File("F:/fip/success");
        FileUtil.moveFolder(x,y);
    }

}
